package chances.wechat.repository;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import chances.wechat.entity.StbInfo;
import chances.wechat.entity.WechatStbBind;
import chances.wechat.entity.WechatUser;
import chances.wechat.service.rdb.BaseDao;

@Repository
public class UserRepository extends BaseDao {
    
    @Value("${stb.desc:%E5%8D%A7%E5%AE%A4}")
    private String stbDesc;
	
	/**
	 * 获取微信用户
	 * @param openId
	 * @return
	 */
	public WechatUser getWechatUser(String openId) {
		return this.findUser(openId);
	}
	
	/**
	 * 查询用户
	 * 
	 * @param stbNo
	 * @param openId
	 * @param value
	 */
	public WechatUser findUser(String openId) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("openId", openId);
		try {
			return (WechatUser) this.queryForObject("getWeixinUserByOpenId", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
		
	}
	
	public void insertWeixinUser(WechatUser user) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("obj", user);
		try {
			this.insert("insertWeiXinUser", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
	}
	
	/**
	 * 更新用户最后一次登录时间
	 * 
	 * @param openId
	 */
	public void updateWeixinUserLoginTime(String openId) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("openId", openId);
		paramsMap.put("time", new Date());
		try {
			this.update("updateWeixinUserLoginTime", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
	}
	
	/**
	 * 更新用户绑定的主机顶盒号
	 * 
	 * @param stbNo
	 */
	public void updateUserMasterStbNo(String stbNo, String openId) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("stbNo", stbNo);
		paramsMap.put("openId", openId);
		try {
			this.update("updateUserMasterStbNo", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
	}
	
	public StbInfo findStbInfo(String stbNo) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("stbNo", stbNo);
		try {
			List<StbInfo> results = (List<StbInfo>) this.queryForList("getStbInfoByStbNo", paramsMap);
			if(!results.isEmpty()) {
				return results.get(0);
			}
			return null;
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
		
	}
	
	public void insertStbInfo(StbInfo stbInfo) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("obj", stbInfo);
		try {
			this.insert("insertStbInfo", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
	}
	
	public WechatStbBind bind(WechatUser aUser, StbInfo stbInfo, String bindId) {
		if (aUser.getId() == null) {
			this.createWeixinUser(aUser);
		}
		/*if (stbInfo.getId() == null) {
			this.insertStbInfo(stbInfo);
		}*/
		WechatStbBind bind = new WechatStbBind(aUser.getOpenId(), stbInfo.getStbNo());
		bind.setRoleName(bindId);
		aUser.setCurrentStbNo(stbInfo.getStbNo());
		bind.setNickname(aUser.getNickname());
		bind.setStatus(WechatUser.ENABLE_STATUS);
		try {
		    stbDesc = URLDecoder.decode(stbDesc,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
		bind.setStbDesc(stbDesc);
		this.insertBindInfo(bind);
		return bind;
	}
	
	/**
	 * 建用户
	 * 
	 * @param wechatUser
	 * @return
	 */
	public void createWeixinUser(WechatUser wechatUser) {
		WechatUser result = this.findUser(wechatUser.getOpenId());
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		if (result == null) {
			paramsMap.put("obj", wechatUser);
			try {
				this.insert("insertWeiXinUser", paramsMap);
			} finally {
				paramsMap.clear();
				paramsMap = null;
			}
			
		}/* else {
			wechatUser.setStatus(WechatUser.ENABLE_STATUS);
			this.update("upateWeixinUserStatus", paramsMap);
		}*/
	}
	
	public void insertBindInfo(WechatStbBind stbInfo) {
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("obj", stbInfo);
		try {
			this.insert("insertStbWeixinBind", paramsMap);
		} finally {
			paramsMap.clear();
			paramsMap = null;
		}
	}

	/*
	*//**
	 * 查询用户机顶盒绑定关系
	 * 
	 * @param stbNo
	 * @param openId
	 * @param value
	 *//*
	public wechatStbBind findBind(String openId, String stb) {
		wechatStbBind info = new wechatStbBind(openId, stb);
		return this.getOne("findBindStbByOpenIdAndStbNo", info);
	}

	*//**
	 * 列出盒子绑定过的机顶盒
	 * 
	 * @param openId
	 * @return
	 *//*
	public List<wechatStbBind> listBindSTB(String openId) {
		return sqlSessionTemplate.selectList(this.getName("findBindStbByOpenId"), openId);
	}
	
	*//**
	 * 查询用户
	 * 
	 * @param stbNo
	 * @param openId
	 * @param value
	 *//*
	public void updateUserProfile(WechatUser user) {
		sqlSessionTemplateMaster.update(this.getNamespaceM("updateWeixinProfile"), user);
	}
	
	
	
	*//**
	 * 更新绑定状态
	 * 
	 * @param openId
	 * @param stbNo
	 * @param value
	 *//*
	public void updateBindStatus(WechatUser user, wechatStbBind stbInfo) {
		user.setCurrentStbNo(stbInfo.getStbNo());
		sqlSessionTemplateMaster.update(this.getNamespaceM("updateBindByOpenIdAndStbNo"), stbInfo);
		sqlSessionTemplateMaster.update(this.getNamespaceM("updateWeixinUserStatus"), user);
		user.setStbNo(stbInfo.getStbNo());
		sqlSessionTemplateMaster.update(this.getNamespaceM("updateDefaultStb"), user);
	}
	
	*//**
	 * 更新角色
	 * 
	 * @param openId
	 * @param stbNo
	 * @param value
	 *//*
	public void updateBindRole(wechatStbBind stbInfo) {
		sqlSessionTemplateMaster.update(this.getNamespaceM("updateBindRoleByStbNoAndOpenId"), stbInfo);
	}
	
	*//**
	 * 微信用户退定
	 * 
	 * @param openId
	 *//*
	public void unSubscribe(String openId) {
		sqlSessionTemplateMaster.delete(this.getNamespaceM("deleteBindByOpenId"), openId);
		sqlSessionTemplateMaster.update(this.getNamespaceM("updateWeixinUserStatusByOpenId"), openId);
	}
	
	public void delBind(Long bindId) {
		sqlSessionTemplateMaster.delete(this.getNamespaceM("deleteBind"), bindId);
	}

	public void updateWeixinUser(WechatUser weixinUser) {
		sqlSessionTemplateMaster.update(this.getNamespaceM("updateWeixinUserStatus"), weixinUser);
	}
	
	*/
	

	/*public List<WeixinUser> listBindWeixin(String stbNo) {
		return sqlSessionTemplate.selectList(this.getName("findBindWeixinByStbNo"), stbNo);
	}*/

	/*public List<StbWeixinBind> listBindByStbNo(String stbNo) {
		return sqlSessionTemplate.selectList(this.getName("findBindByStbNo"), stbNo);
	}*/

	/**
	 * 更新绑定状态
	 * 
	 * @param openId
	 * @param stbNo
	 * @param value
	 */
	/*public void updateMasterStbNo(WeixinUser user) {
		sqlSessionTemplateMaster.update(this.getNamespaceM("updateDefaultStb"), user);
	}*/

	/**
	 * 解绑盒子
	 * 
	 * @param openId
	 * @param stb
	 */
	/*public void unBind(String openId, String stb) {
		StbWeixinBind stbInfo = new StbWeixinBind(openId, stb);
		stbInfo.setStatus(WeixinUser.DISABLE_STATUS);
		sqlSessionTemplate.update(this.getName("updateStbInfoByOpenIdAndStbNo"), stbInfo);
	}*/


	/*public void updateStbLoginTime(StbInfo stbInfo) {
		sqlSessionTemplateMaster.update(this.getNamespaceM("updateStbInfo"), stbInfo);
	}*/

	/*public void disableUser(WeixinUser aUser) {
		aUser.setStatus(WeixinUser.DISABLE_STATUS);
	}*/
	
	
	@SuppressWarnings("unchecked")
	public List<WechatUser> getAllWechatUser() {
		try {
			return (List<WechatUser>)this.queryForList("getAllWechatUser");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
